# 显示怪物信息的窗口
class Window_EnemyInfo < Window_Selectable
  # 初始化
  def initialize
    super(160, 0, 480, 480)
    @column_max = 1
    @data = []
    refresh
    self.index = 0
    self.active = true
  end

  # 获取开头行
  def top_row
    self.oy / 64
  end

  # 设置开头行
  def top_row=(row)
    row = [row, 0].max
    row = [row, row_max - 1].min
    self.oy = row * 64
  end

  # 获取一页可以显示的行数
  def page_row_max
    (self.height - 32) / 64
  end

  # 更新光标矩形
  def update_cursor_rect
    # 光标位置不满0的情况下
    if @index < 0
      self.cursor_rect.empty
      return
    end
    # 获取当前行
    row = @index / @column_max
    # 当前行在开头行前，向开头滚动
    self.top_row = [self.top_row, row].min
    # 当前行在末尾行后，向末尾滚动
    self.top_row = [self.top_row, row - self.page_row_max + 1].max
    # 计算光标的宽
    cursor_width = self.width / @column_max - 32
    # 计算光标坐标
    x = @index % @column_max * (cursor_width + 32)
    y = @index / @column_max * 64 - self.oy
    # 更新光标矩形
    self.cursor_rect.set(x, y, cursor_width, 64)
  end

  # 刷新
  def refresh
    if self.contents != nil
      self.contents.dispose
      self.contents = nil
    end
    # 记录地图上的怪物
    @data = EnemyInfo.getSort
    # 如果没有怪物则不显示
    if @data.empty?
      self.index = -1
      self.active = false
      return
    end
    # 生成位图、描绘项目
    @item_max = @data.size
    self.contents = Bitmap.new(width - 32, row_max * 64)
    for i in 0...@item_max
      draw_item(i)
    end
  end

  # 描绘项目
  def draw_item(index)
    y = index * 64
    einfo = Game_EnemyInfo.new(@data[index])
    # 描画图标
    bitmap = RPG::Cache.battler($data_enemies[@data[index]].battler_name, $data_enemies[@data[index]].battler_hue)
    self.contents.blt(16, y + 16, bitmap, Rect.new(0, 0, bitmap.width, bitmap.height))   
    bitmap = RPG::Cache.icon("hp")
    self.contents.blt(88, y + 32, bitmap, Rect.new(0, 0, bitmap.width, bitmap.height))   
    bitmap = RPG::Cache.icon("atk")
    self.contents.blt(208, y + 32, bitmap, Rect.new(0, 0, bitmap.width, bitmap.height))
    bitmap = RPG::Cache.icon("def")
    self.contents.blt(328, y + 32, bitmap, Rect.new(0, 0, bitmap.width, bitmap.height))
    bitmap = RPG::Cache.icon("skill")
    self.contents.blt(88, y, bitmap, Rect.new(0, 0, bitmap.width, bitmap.height))
    bitmap = RPG::Cache.icon("gold")
    self.contents.blt(208, y, bitmap, Rect.new(0, 0, bitmap.width, bitmap.height))
    bitmap = RPG::Cache.icon("damage")
    self.contents.blt(328, y, bitmap, Rect.new(0, 0, bitmap.width, bitmap.height))
    # 描画数值
    self.contents.font.color = normal_color
    self.contents.draw_text(112, y + 32, 80, 24, einfo.ehp.to_s, 2)
    self.contents.draw_text(232, y + 32, 80, 24, einfo.eatk.to_s, 2)
    self.contents.draw_text(352, y + 32, 80, 24, einfo.edef.to_s, 2)
    self.contents.draw_text(232, y, 80, 24, $data_enemies[@data[index]].gold.to_s, 2)
    
    if einfo.eskill > 0
      self.contents.font.color = text_color(3)
      self.contents.draw_text(112, y, 80, 24, $data_skills[einfo.eskill].name, 2)
    else
      self.contents.font.color = text_color(7)
      self.contents.draw_text(112, y, 80, 24, "无技能", 2)
    end

    d = einfo.getDamage
    if d == -1
      self.contents.font.color = text_color(7)
      self.contents.draw_text(352, y, 80, 24, "????", 2)
    else
      self.contents.font.color = text_color(2)
      self.contents.draw_text(352, y, 80, 24, d.to_s, 2)
    end
  end
end
